<?php
// 本类由系统自动生成，仅供测试用途
namespace Admin\Controller;

class WebSetController extends LoginController {
	public $re_url='/admin/WebSet/baseset';

	
	public function __construct(){
		parent::__construct();
		layout(false);
	}
	
    //网站基本信息设置
    public function baseset(){
    	if($_POST['set_config']){
    		
    		$set_config=$_POST['set_config'];
    		
    		$set_config1=require APP_PATH."/Common/Conf/setting.config.php";
    		
    		$set_config=array_merge($set_config1,$set_config);//合并配置数组
    		$set_config = "<?php \n return \n".var_export($set_config,true)." \n ?>";
    		file_put_contents(APP_PATH."/Common/Conf/setting.config.php",$set_config); //通过file_put_contents保存
    		
    		$set_lang=$_POST['set_lang'];
    		
    		$set_lang1=require APP_PATH."/Common/Lang/set.lang.php";
    		$set_lang=array_merge($set_lang1,$set_lang);//合并语言数组
    		
    		$set_lang = "<?php \n return \n".var_export($set_lang,true)." \n ?>";
    		file_put_contents(APP_PATH."/Common/Lang/set.lang.php",$set_lang); //通过file_put_contents保存
    		redirect($this->re_url,0, '页面跳转中...');
    		
    	}else{
    		$siteconfig  = require APP_PATH.'/Common/Conf/setting.config.php';
    		$this->assign('siteconfig',$siteconfig);
    		
    		$this->display();
    	}
    	
    
    }


      //网站基本信息
      public function index(){
      	$db=D("Webinfo");
      	    if(IS_POST){
      	    	$this->checkToken($_POST);
      	    	$data=$db->webset($_POST);//修改、新增数据
      	    }else{
      	    	$data=$db->getWebinfo();
      	    }
      			$this->assign("config",$data);
      	        $this->display();
      }

    //删除网站基本信息
    public function webDe(){
    	$db=D("Webinfo");
    	if(IS_POST){
    		echo  $db->delete($_POST['id']);
    	}
    }

    //修改网站基本信息
    public function webUp(){
    	$db=D("Webinfo");
    	if(IS_POST){
    		$this->ajaxReturn($db->find($_POST['id'])) ;
    	}
    }
    //栏目管理
    
    public function cate(){
    	
    	$this->db = D('Cate');
    	
    	if($_POST){
    		$this->checkToken($_POST);
    		$arr=$_POST;
    		if($arr['id']){
    			$this->db->where("id=".$arr['id'])->save($arr);
    		}else{
    			$this->db->add($arr);
    		}
    		S('cateinfo',null);
    	}
    	
    	$data=$this->db->get_cate();
    	$data=genTree($data);
    	$this->db =D("Moxing");
    	$modelinfo=$this->db->getModel();
    	$this->assign("data",$data);
    	$this->assign("modelinfo",$modelinfo);
    	$this->display();
    	
    }
    
    
    /**
     * 删除栏目
     * @access public
     * @param int $cat_id 栏目id
     */
    public function cateDe(){
    	if(IS_POST){
    		$cat_id=$_POST['id'];
    		$this->db=D('Cate');
    		
    		$this->db->where("id=$cat_id")->delete();
    		$this->deleteCache();
    	}
    
    }
    
    /**
     *栏目排序
     * @access public
     * @param int $id 栏目id
     * @param int $cat_id 栏目id
     */
    public function listorderCate(){
    	$arr=$_POST;
    	$arr=$arr['listorder'];
    	$this->db=D("Cate");
    	foreach ($arr as $key=>$r){
    		$this->db->where("id=$key")->save(array('listorder'=>$r));
    	}
    	$this->deleteCache();
     redirect($_SERVER['HTTP_REFERER']);
    }
    
    
     /**
     * 单条栏目信息 
     * @access public
     * @param int $cat_id 栏目id
     */
    public function cateOne(){
    	$id=$_POST['id'];
    	$this->db=D('Cate');
    	 $data=$this->db->where("id=$id")->select();
    	 $this->ajaxReturn($data[0]);
    }
    
   /**
     * 栏目文章管理 
     * @access public
     * @param int $cat_id 栏目id
     */
    
    public function article(){
    	$cate_id=I('get.cate_id');
    	$cateinfo=$this->getCateInfo();
    	
    	$Article = D($cateinfo[$cate_id]['table']); // 实例化Data数据对象
    	$count      = $Article->where("cate_id=$cate_id")->count();// 查询满足要求的总记录数 $map表示查询条件
    	$Page =page($count, 10);
    	$show       = $Page->show();// 分页显示输出
    	// 进行分页数据查询
    	$orderby['id']='desc';
    	$list = $Article->where("cate_id=$cate_id")->field("id,title,user,cate_id,up_date,listorder")->order($orderby)->limit($Page->firstRow.','.$Page->listRows)->select();
    	$this->assign('list',$list);// 赋值数据集
    	$this->assign('show',$show);// 赋值分页输出
    	$this->assign('cate',$cateinfo[$cate_id]);
    	$this->assign('cateinfo',$cateinfo);
    	$this->display(); // 输出模板
    	
    	
    }
    
    //添加修改文章
    public function articleUp(){
    	$cate_id=I('post.cate_id');
    	$cateinfo=$this->getCateInfo();
    		//读取修改文章
    		$id=I('post.id');
    		$this->db= D($cateinfo[$cate_id]['table']); // 实例化Data数据对象
    		$article=$this->db->find($id);
    	$this->ajaxReturn($article);
    }
    
    public function articleRead(){
    	$cate_id=I('get.cate_id');
    	$id=I('get.id');
    	$data="";
    	$cateinfo=$this->getCateInfo($cate_id);
    	if($id){
    		$this->db=D($cateinfo['table']);
    		$data=$this->db->where("id=$id")->select();
    	}
    	$this->assign(array(
    			'data'=>$data[0],
    			'cate'=>$cateinfo,
    	));
    	$this->display("Models:".$cateinfo['show']);
    }
    
    //保存文章
    public function articleSave(){
    	
    	//表单令牌环检查
    	$this->checkToken($_POST);
    	
    	$arr=$_POST;
    	$position=$arr['p'];
    	unset( $arr['p'] );
    	$cate_id=$arr['cate_id'];
    	
    	$cateinfo=$this->getCateInfo($cate_id);
    	$this->db= D($cateinfo['table']); // 实例化Data数据对象
    	$db=D("position_data");
    	if($arr["id"]){
    		$id=$arr['id'];
    		//修改文章
    		$arr['up_date']=date("Y-m-d H:i:s");
    		$this->db->save($arr);
    		$db->where("cate_id=$cate_id and data_id=$id")->delete();
    	}else{
    		//新增文章
    		$arr['creat_date']=date("Y-m-d H:i:s");
    		$arr['up_date']=date("Y-m-d H:i:s");
    		$arr['user']=$this->user_id;
    		$id=$this->db->add($arr);
    		$this->db->where("id=$id")->save(array("listorder"=>$id));
    	}
    	//更新推进位数据
    	foreach ($position as $r){
    		$addArr=array(
    				'p_id'=>$r,
    				'cate_id'=>$cate_id,
    				'data_id'=>$id,
    		);
    		$db->add($addArr);
    	}
    	$this->success("保存成功！",$_SERVER['HTTP_REFERER'],5);
    }
   
    	
    	/**
    	 * 文章排序
    	 * @access public
    	 * @param int $id 文章id
    	 * @param int $cat_id 栏目id
    	 */
    	public function listorder(){
    		$arr=$_POST;
    		$cate_id=$arr['cate_id'];
    		$arr=$arr['listorder'];
    		$cateinfo=$this->getCateInfo($cate_id);
    		$this->db=D($cateinfo['table']);
    		foreach ($arr as $key=>$r){
    			$this->db->where("id=$key")->save(array('listorder'=>$r));
    		}
    		redirect($_SERVER['HTTP_REFERER']);
    	}
    	//多选删除
    	public function dataDe(){
    		$id=$_POST['dataDe'];
    		$catid=$_POST['cate_id'];
    		$where="";
    		foreach ($id as $r){
    			$where =$where.$r.",";
    		}
    		$where=substr($where,0,strlen($where)-1);
    		$cateinfo=$this->getCateInfo($catid);
    		$this->db=D($cateinfo['table']);
    		$this->db->where("id in ($where)")->delete();
    		redirect($_SERVER['HTTP_REFERER']);
    	}
   
  
    //清空缓存
  public function deleteCache(){
  	 deldir(APP_PATH."Runtime");
  	 echo true;
  }
    
 
}